﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace slCommunicator
{
    public partial class SettingsForm : Form
    {
        private static PropertyInfo[] settingsProperties = Properties.Settings.Default.GetType().GetProperties();
        public SettingsForm()
        {
            InitializeComponent();

            propertyGrid.SelectedObject = Properties.Settings.Default;
        }

        public static SettingsForm ShowMe()
        {
            if (Application.VisualStyleState != System.Windows.Forms.VisualStyles.VisualStyleState.ClientAreaEnabled)
            Application.EnableVisualStyles();

            SettingsForm sf = new SettingsForm();
            Application.Run(sf);
            return sf;
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.UserClosing)
                return;
            var result = MessageBox.Show(localization.msgbox_exitSettingsText, localization.msgbox_exitSettingsTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            switch (result)
            {
                case System.Windows.Forms.DialogResult.Yes:
                    Properties.Settings.Default.Save();
                    System.Diagnostics.Process.Start(Application.ExecutablePath, "restart " + System.Diagnostics.Process.GetCurrentProcess().Id);
                    Environment.Exit(-1);
                    break;
                case System.Windows.Forms.DialogResult.No:
                    break;
                case System.Windows.Forms.DialogResult.Cancel:
                    e.Cancel = true;
                    break;
            }
        }

        private void propertyGrid_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
        {
            string propName = e.NewSelection.Label;
            var prop = settingsProperties.First(x => x.Name == propName);
            if (prop == null)
                return;

            string description = (prop.GetCustomAttribute(typeof(System.Configuration.SettingsDescriptionAttribute)) as System.Configuration.SettingsDescriptionAttribute).Description;
            setDescription(description);
        }

        private Label descLabel = null;
        private void FindDescLabel()
        {
            if (descLabel != null)
                return;
            Control dc = null;
            foreach (Control control in propertyGrid.Controls)
            {
                if (control.GetType().Name == "DocComment")
                {
                    dc = control;
                    break;
                }
            }
            if (dc == null)
                return;

            foreach (Control control in dc.Controls)
            {
                if (control is Label && !string.IsNullOrEmpty(control.Text))
                {
                    descLabel = (Label)control;
                    break;
                }
            }
        }

        private void setDescription(string text)
        {
            FindDescLabel();
            if (descLabel == null)
                return;

            descLabel.Text = text;
        }
    }
}
